package com.yjf.indoormap.model;

import com.yjf.indoormap.db.GreenDaoUtils;
import com.yjf.indoormap.entity.Shop;
import com.yjf.indoormap.greenDao.ShopDao;
import com.yjf.indoormap.network.MyResult;
import com.yjf.simple_retrofit.simples.ResultObserver;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by yjf on 2017/4/22.
 */

public class LocalShopModel implements ShopModel {

    private ShopDao shopDao;

    public LocalShopModel() {
        shopDao = GreenDaoUtils.getSingleTon().getSession().getShopDao();
    }

    @Override
    public void searchByKey(final String type, ResultObserver<MyResult<List<Shop>>> observer) {

        Observable.create(new ObservableOnSubscribe<MyResult<List<Shop>>>() {
            @Override
            public void subscribe(ObservableEmitter<MyResult<List<Shop>>> e) throws Exception {

                List<Shop> shopList = shopDao.queryBuilder()
                        .where(ShopDao.Properties.Type.eq(type))
                        .list();
                e.onNext(new MyResult<List<Shop>>(1, "success").setData(shopList));
                e.onComplete();

            }
        })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(observer);

    }

    @Override
    public void loadAll(ResultObserver<MyResult<List<Shop>>> observer) {

        Observable.create(new ObservableOnSubscribe<MyResult<List<Shop>>>() {
            @Override
            public void subscribe(ObservableEmitter<MyResult<List<Shop>>> e) throws Exception {


                List<Shop> shopList = shopDao.loadAll();
                e.onNext(new MyResult<List<Shop>>(1, "success").setData(shopList));
                e.onComplete();

            }
        })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(observer);

    }

    @Override
    public void loadByType(final String type, ResultObserver<MyResult<List<Shop>>> observer) {

        Observable.create(new ObservableOnSubscribe<MyResult<List<Shop>>>() {
            @Override
            public void subscribe(ObservableEmitter<MyResult<List<Shop>>> e) throws Exception {

                List<Shop> shopList = shopDao.queryBuilder()
                        .where(ShopDao.Properties.Type.eq(type))
                        .list();
                e.onNext(new MyResult<List<Shop>>(1, "success").setData(shopList));
                e.onComplete();
            }
        })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(observer);
    }

    @Override
    public void loadRecommended(int index, ResultObserver<MyResult<List<Shop>>> observer) {
        Observable.create(new ObservableOnSubscribe<MyResult<List<Shop>>>() {
            @Override
            public void subscribe(ObservableEmitter<MyResult<List<Shop>>> e) throws Exception {


                List<Shop> shopList = shopDao.queryBuilder().orderDesc(ShopDao.Properties.Level)
                        .list();
                e.onNext(new MyResult<List<Shop>>(1, "success").setData(shopList));
                e.onComplete();

            }
        })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(observer);
    }
}
